package com.huangyi;

public class Main {
    public static void main(String[] args) {
       //最大连续 1 的个数
        class Solution {
            public int longestOnes(int[] nums, int k) {
                int ret =0;
                for(int left = 0,right =0,sum = 0;right<nums.length;right++){
                    if(nums[right] == 0 ) sum++;
                    while(sum>k){
                        if(nums[left++] == 0){sum--;}
                    }
                    ret = Math.max(ret,right-left+1);
                }
                return ret;
            }
        }



        //将 x 减小到 0 的最小操作数
        class Solution2 {
            public int minOperations(int[] nums, int x) {
                int ret = Integer.MAX_VALUE ,n=nums.length,target =0,tmp = 0;
                for(int a:nums){tmp +=a;}
                target = tmp - x;
                if(target<0){return -1;}
                for(int left = 0,right =0,sum = 0;right<n;right++){
                    sum += nums[right];
                    while(sum> target){
                        sum -= nums[left++];
                    }
                    if(sum == target){
                        ret = Math.min(ret,nums.length-(right-left+1));
                    }
                }
                return ret == Integer.MAX_VALUE? -1 : ret;
            }
        }
    }
}